<?php
class youkaModel extends model{
	public $base;
	function __construct(&$base){
		parent::__construct($base);
		$this->base=$base;
		$this->table="youka";
	}
	
	public function get($userid=0,$typeid=1){
		if(!$userid) $userid=M("login")->userid;
		if($userid){
			if($typeid==1){
				$uka=$this->selectRow("userid=".$userid);
				if(!$uka){
					$this->insert(array(
						"userid"=>$userid,
						"sxmoney"=>YOUKA_GR_SXMONEY	,
						"money"=>YOUKA_GR_SXMONEY,				
					));
				}
				return $this->selectRow("userid=".$userid);
			}elseif($typeid==2){
				$sushe=M("sushe_user")->selectRow("userid=".$userid);
				 
				if($sushe){
					$uka=$this->selectRow("sushe=".$sushe['sushe']);
					if(empty($uka)){
						$this->insert(array(
							"money"=>YOUKA_QS_SXMONEY,
							"sxmoney"=>YOUKA_QS_SXMONEY,
							"buy_money"=>0,
							"sushe"=>$sushe['sushe'],
						));
					}
					return $this->selectRow("sushe=".$sushe['sushe']);
				}
			}
		}
	}
	
	/**
		收入支出处理
		$option=array(
			"userid"=>1,
			"money"=>$money,
			"content"=>"您获得了[money]元",
			"siteid"=>SITEID,
			"typeid"=>1
		) 	
	**/
	
	public function addMoney($option){
		
		$userid=$option['userid'];
		if(isset($option['typeid'])){
			$typeid=$option['typeid'];
		}else{
			$typeid=1;
		}
		if(isset($option['youka'])){
			$kaid=$option['youka'];
		}else{
			$kaid=0;
		}
		if($typeid==2){
			if($kaid){
				$row=$this->selectRow("id=".$kaid);
			}else{
				$sushe=M("sushe_user")->selectRow("userid=".$userid);
				if($sushe){
					$row=$this->selectRow("sushe=".$sushe['sushe']);
				}
			}
		}else{
			$row=$this->selectRow("userid=".$userid);
		}
		if(empty($row)) return false;
		$data=array();
		if(isset($option['money'])){
			$data['money']=$row['money']+$option['money'];
			
		}
		if(isset($option['siteid'])){
			$siteid=$option['siteid'];			
		}else{
			$siteid=SITEID;
		}
		 
		 
		$this->update($data,"id=".$row['id']);
		 
		$logdata=array(
				"dateline"=>time(),
				"userid"=>$userid,
				"typeid"=>$typeid,
				"siteid"=>$siteid,				 
		);
		
		
		 
		
		if(isset($option['money'])){
			 
			$logdata['money']=$option['money'];
			$logdata['content']=str_replace("[money]",$option['money'],$option['content'])." 原来".$row['money']."元，现在".$data['money']."元";
			M("youka_log")->insert($logdata);
		}
		
	}
	
	 
	
}
?>